﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using UtfprLibrary.Windows.Forms.ComponentModel;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;

namespace UtfprLibrary.Windows.Forms {
    /// <summary>
    /// TODO
    /// </summary>
    [ToolboxBitmap(typeof(ResourceFinder), "Resources.VSD_UBNRequiredFieldsValidator.png")]
    public class UBNRequiredFieldsValidator : UBNRequiredFieldsValidatorBase {
        #region Base

        /// <summary>
        /// TODO
        /// </summary>
        /// <returns></returns>
        protected override Dictionary<Control, bool> EvaluateAreValid() {
            var _areValid = new Dictionary<Control, bool>();

            foreach (UtfprValidatableControl item in ValidatableControlCollection) {
                bool evaluation = false;

                if (item.Control is UtfprDateTimePickerBase) {
                    evaluation = evaluateIsValid_UtfprDateTimePickerBase(item.Control);
                } else if (item.Control is UtfprLookupFieldBase) {
                    evaluation = evaluateIsValid_UtfprLookupFieldBase(item.Control);
                } else if (item.Control is UtfprGroupBoxBase) {
                    evaluation = evaluateIsValid_UtfprGroupBoxBase(item.Control);
                } else if (item.Control is ComboBox) {
                    evaluation = evaluateIsValid_ComboBox(item.Control);
                } else if (item.Control is TextBox || item.Control is MaskedTextBox) {
                    evaluation = evaluateIsValid_TextBox(item.Control);
                }
                
                _areValid.Add(item.Control, evaluation);
            }

            return _areValid;
        }
               
        #region Properties

        private UBNValidatableControlCollection _validatableControlCollection;
        /// <summary>
        /// TODO
        /// </summary>
        [Category("Behavior")]
        [Description("Coleção de controles a serem validados")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //[Editor("UtfprLibrary.Windows.Forms.ComponentModel.UBNValidatableControlCollectionEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public override UBNValidatableControlCollection ValidatableControlCollection {
            get {
                if (_validatableControlCollection == null) {
                    _validatableControlCollection = new UBNValidatableControlCollection();
                }
                return _validatableControlCollection; 
            }
            set { _validatableControlCollection = value; }
        }

        #endregion

        #endregion

        #region Methods

        private bool evaluateIsValid_TextBox(Control control) {
            return control.Text.Trim() != "";
        }

        private bool evaluateIsValid_ComboBox(Control control) {
            return (control as ComboBox).SelectedIndex != -1;
        }

        private bool evaluateIsValid_UtfprDateTimePickerBase(Control control) {
            var format = (control as UtfprDateTimePickerBase).Format;
            var customFormat = (control as UtfprDateTimePickerBase).CustomFormat;

            return !(format == DateTimePickerFormat.Custom && customFormat == " ");
        }

        private bool evaluateIsValid_UtfprLookupFieldBase(Control control) {
            //get the field info
            var info = control.GetType().GetField(
                UtfprWindowsFormsConsts.UtfprLookupField_TextBoxKeyMember_FieldName, 
                BindingFlags.Instance | BindingFlags.NonPublic);

            //checked if the field value is empty
            return (info.GetValue(control) as TextBox).Text.Trim() != "";
        }

        private bool evaluateIsValid_UtfprGroupBoxBase(Control control) {
            //verifica se existe algum UtfprRadioButtonBase checked no UtfprGroupBox
            bool containsRBChecked = true;
            bool containsCBChecked = true;
            //pressupondo que exista um TableLayoutPanel dentro dele

            List<UtfprRadioButtonBase> radioButtons;
            List<UtfprCheckBoxBase> checkBoxes;

            if (control.Controls.OfType<TableLayoutPanel>().Count() == 1) {
                radioButtons = control.Controls.OfType<TableLayoutPanel>().Single().Controls.OfType<UtfprRadioButtonBase>().ToList();
                checkBoxes = control.Controls.OfType<TableLayoutPanel>().Single().Controls.OfType<UtfprCheckBoxBase>().ToList();
            } else {
                radioButtons = control.Controls.OfType<UtfprRadioButtonBase>().ToList();
                checkBoxes = control.Controls.OfType<UtfprCheckBoxBase>().ToList();
            }
                                   
            if (radioButtons.Count > 0) {
                containsRBChecked = radioButtons.Where(rb => rb.Checked).Count() > 0;
            }

            if (checkBoxes.Count > 0) {
                containsCBChecked = checkBoxes.Where(cb => cb.Checked).Count() > 0;
            }

            return containsRBChecked && containsCBChecked;
        }

        #endregion
    }
}
